﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.SelfHost;

namespace ConsoleSelf
{
    class Program
    {
        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();// 获取控制台句柄

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_HIDE = 0;// 隐藏
        const int SW_SHOW = 5;// 显示
        static void Main(string[] args)
        {
            var config = new HttpSelfHostConfiguration("http://localhost:8084");

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });
            using (var server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();
                Console.WriteLine("Press Enter to quit.");
                Console.WriteLine("三秒之后窗口被隐藏！！");
                for (int i = 3; i >= 1; i--)
                {
                    Console.WriteLine(i);
                    Thread.Sleep(1000);
                }
                ShowWindow(GetConsoleWindow(), SW_HIDE);
                Console.ReadLine();
            }
        }
    }
}
